package com.example.demo.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.demo.mapper.StudentAttributeMapper;
import com.example.demo.mapper.StudentBadgeMapper;
import com.example.demo.mapper.StudentCouponsMapper;
import com.example.demo.mapper.StudentTaskMapper;
import com.example.demo.mapper.entity.*;
import com.example.demo.service.StudentTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Lucy
 * @create 2024-02-15 15:26
 */
@Service
public class StudentTaskServiceImpl implements StudentTaskService {

    @Autowired
    private StudentTaskMapper mapper;

    @Autowired
    private StudentAttributeMapper attributeMapper;

    @Autowired
    private StudentBadgeMapper badgeMapper;

    @Override
    public Task selectTaskById(int taskId) {
        return mapper.selectTaskById(taskId);
    }

    @Override
    public List<Task> getMORSTasksList(int isMainQuest) {
        return mapper.getMORSTasksList(isMainQuest);
    }

    @Override
    public List<StudentTask> selectStudentTasksListByStudentId(int studentId) {
        return mapper.selectStudentTasksListByStudentId(studentId);
    }

    @Override
    public List<Question> selectQuestionListByBankId(int bankId) {
        return mapper.selectQuestionListByBankId(bankId);
    }

    @Override
    public void insertStudentAnswerRecord(StudentAnswerRecord record) {
        mapper.insertStudentAnswerRecord(record);
    }

    @Override
    public void updateCompletionStatus(int studentId, int taskId, int status) {
        mapper.updateCompletionStatus(studentId, taskId, status);
    }

    @Override
    public Item selectItemById(int itemId) {
        return mapper.selectItemById(itemId);
    }

    @Override
    public void insertCheckinImage(CheckinImage image) {
        mapper.insertCheckinImage(image);
    }

    @Override
    public void insertCheckinImageFileName(int imageId, String fileName) {
        mapper.insertCheckinImageFileName(imageId, fileName);
    }

    @Override
    public void uploadCheckinComments(int studentId, int taskId, String comments, int status) {
        mapper.uploadCheckinComments(studentId, taskId, comments, status);
    }

    @Override
    public void receiveTaskReward(int studentId, int taskId) {
        Task task = mapper.selectTaskById(taskId);
        String attributesAwarded = task.getAttributesAwarded();
        System.out.println("属性获得" + attributesAwarded);
        // 将字符串解析为 JSON 对象
        JSONObject jsonData = JSONUtil.parseObj(attributesAwarded);
        // 遍历 JSON 对象的键值对
        for (String attributeName : jsonData.keySet()) {
            Integer value = jsonData.getInt(attributeName);
            Attribute attribute = attributeMapper.selectAttributeByName(attributeName);
            attributeMapper.updateStudentAttribute(studentId, attribute.getAttributeId(), value);
        }
        StudentBadge studentBadge = new StudentBadge();
        studentBadge.setStudentId(studentId);
        studentBadge.setBadgeId(task.getBadgeAwarded());
        if(task.getBadgeAwarded() != 0) badgeMapper.insertStudentBadge(studentBadge);

        //检查特殊徽章是否有增加
        List<StudentBadge> studentBadgeList = badgeMapper.selectStudentBadgeById(studentId);
        List<Badge> badgeSpecialList = badgeMapper.selectTypeBadge(3);
        Collections.sort(badgeSpecialList, (o1, o2) -> Integer.compare(o2.getBadgeNumber(), o1.getBadgeNumber()));
        int badgeNumber = badgeMapper.selectBadgeNumber(studentId);

        Set<Integer> existingBadgeIds = studentBadgeList.stream()
                .map(StudentBadge::getBadgeId)
                .collect(Collectors.toSet());

        for (Badge badge : badgeSpecialList) {
            if (badgeNumber >= badge.getBadgeNumber() && !existingBadgeIds.contains(badge.getBadgeId())) {
                StudentBadge studentBadge3 = new StudentBadge();
                studentBadge3.setBadgeId(badge.getBadgeId());
                studentBadge3.setStudentId(studentId);
                badgeMapper.insertStudentBadge(studentBadge3);
                break;
            }
        }

        //检查成长徽章是否有增加
        List<Badge> badgeGrowthList = badgeMapper.selectTypeBadge(1);
        List<StudentAttribute> studentAttributeList = attributeMapper.getStudentAttributeList(studentId);
        for(StudentAttribute studentAttribute : studentAttributeList) {
            int givenAttributeId = studentAttribute.getAttributeId();
            int attributeValue = studentAttribute.getValue();
            List<Badge> filteredAndSortedBadges = badgeGrowthList.stream()
                    .filter(badge -> badge.getAttributeId() == givenAttributeId)
                    .sorted(Comparator.comparingInt(Badge::getAttributeValue).reversed())
                    .collect(Collectors.toList());
            for(Badge badge : filteredAndSortedBadges){
                if(attributeValue >= badge.getAttributeValue() && !existingBadgeIds.contains(badge.getBadgeId())){
                    StudentBadge studentBadge2 = new StudentBadge();
                    studentBadge2.setBadgeId(badge.getBadgeId());
                    studentBadge2.setStudentId(studentId);
                    badgeMapper.insertStudentBadge(studentBadge2);
                    break;
                }
            }
        }
    }

    @Override
    public StudentTask selectStudentTask(int studentId, int taskId) {
        return mapper.selectStudentTask(studentId, taskId);
    }

    @Override
    public CheckinImage selectCheckinImage(int studentId, int taskId, int itemId) {
        return mapper.selectCheckinImage(studentId, taskId, itemId);
    }

    @Override
    public void updateCheckinStatus(CheckinImage image) {
        mapper.updateCheckinStatus(image);
    }
}
